package bufferings.lab.basic.check;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.EntityQuery;
import org.slim3.tester.AppEngineTestCase;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query.FilterOperator;

public class MyGeoPt extends AppEngineTestCase {

  @Before
  public void setUp() throws Exception {
    super.setUp();
    putEntity("lat00", "lon00");// 0000
    putEntity("lat00", "lon10");// 0010
    putEntity("lat00", "lon20");// 0020
    putEntity("lat10", "lon00");// 1000
    putEntity("lat10", "lon10");// 1010
    putEntity("lat10", "lon20");// 1020
    putEntity("lat20", "lon00");// 2000
    putEntity("lat20", "lon10");// 2010
    putEntity("lat20", "lon20");// 2020
    
    // lat00
    // lat10
    // lat10
    // lat10
    // lat10
  }

  @Test
  public void sample() {
    EntityQuery query = Datastore.query("MyGeoPt");
    query.filter("geo", FilterOperator.GREATER_THAN, "lat05");
    query.filter("geo", FilterOperator.GREATER_THAN, "lat15");
//    query.filter("geo", FilterOperator.LESS_THAN, "lon05");
    query.filter("geo", FilterOperator.LESS_THAN, "lon15");

//    query.filter("geo", FilterOperator.GREATER_THAN, "lat05");
//    query.filter("geo", FilterOperator.LESS_THAN, "lat15");
//    query.filter("geo", FilterOperator.GREATER_THAN, "lon05");
//    query.filter("geo", FilterOperator.LESS_THAN, "lon15");
    List<Entity> entities = query.asList();

    System.out.println(entities);
  }

  private void putEntity(String lat, String lon) {
    List<String> geoList = new ArrayList<String>();
    geoList.add(lat);
    geoList.add(lon);

    Entity entity = new Entity("MyGeoPt");
    entity.setProperty("geo", geoList);
    Datastore.put(entity);
  }
}
